<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis中Lua脚本的原子性保证</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 4s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.3);
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .image-container {
            position: relative;
            overflow: hidden;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .image-container img {
            transition: transform 0.3s ease;
        }
        
        .image-container:hover img {
            transform: scale(1.05);
        }
        
        .mermaid {
            background: #f8f9fa;
            padding: 2rem;
            border-radius: 12px;
            margin: 2rem 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 1.5rem;
            border-radius: 8px;
            margin: 1.5rem 0;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="text-center">
                <div class="floating-icon inline-block mb-6">
                    <i class="fas fa-lock text-6xl opacity-80"></i>
                </div>
                <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                    Redis中Lua脚本的原子性保证
                </h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    深入理解Redis如何通过单线程模型和事务机制，确保Lua脚本执行的完整性与一致性
                </p>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <div class="flex items-start space-x-4">
                <div class="feature-icon text-3xl">
                    <i class="fas fa-info-circle"></i>
                </div>
                <div class="flex-1">
                    <p class="text-lg leading-relaxed text-gray-700">
                        Lua脚本在Redis中的执行是原子的，这是由于Redis的单线程执行模型以及Lua脚本的执行方式所致。这种原子性保证了脚本执行的完整性和一致性，使其成为处理复杂事务的理想选择。
                    </p>
                </div>
            </div>
        </div>

        <!-- Concept Visualization -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                <i class="fas fa-sitemap mr-3 text-purple-600"></i>核心概念关系图
            </h2>
            <div class="mermaid">
                graph TD
                    A[Redis原子性保证] --> B[单线程模型]
                    A --> C[Lua脚本整体性]
                    A --> D[事务队列机制]
                    A --> E[执行期间锁定]
                    A --> F[不可中断性]
                    
                    B --> B1[命令顺序执行]
                    B --> B2[无并发冲突]
                    
                    C --> C1[全部成功]
                    C --> C2[全部失败]
                    
                    D --> D1[脚本入队]
                    D --> D2[顺序执行]
                    
                    E --> E1[数据锁定]
                    E --> E2[防止修改]
                    
                    F --> F1[错误处理]
                    F --> F2[一致性保证]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Feature Cards Grid -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <!-- Lua脚本的整体性 -->
            <div class="content-card rounded-2xl shadow-xl p-8">
                <div class="flex items-center mb-6">
                    <div class="feature-icon text-4xl mr-4">
                        <i class="fas fa-cube"></i>
                    </div>
                    <h2 class="section-title text-2xl font-bold text-gray-800">Lua脚本的整体性</h2>
                </div>
                <p class="text-gray-700 leading-relaxed mb-6 text-indent">
                    在Redis中执行Lua脚本是一个原子操作，Lua脚本会作为整体被执行，不会被其他Redis操作中断。这意味着，一个Lua脚本内的所有指令要么全部执行成功，要么全部失败，保证了整体性。
                </p>
                <div class="image-container">
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1706002193825-6210aa7b-9778-472c-a6c8-bd321272f669.png" 
                         alt="Lua脚本整体性示意图" class="w-full">
                </div>
            </div>

            <!-- Redis的单线程模型 -->
            <div class="content-card rounded-2xl shadow-xl p-8">
                <div class="flex items-center mb-6">
                    <div class="feature-icon text-4xl mr-4">
                        <i class="fas fa-microchip"></i>
                    </div>
                    <h2 class="section-title text-2xl font-bold text-gray-800">Redis的单线程模型</h2>
                </div>
                <p class="text-gray-700 leading-relaxed mb-6 text-indent">
                    Redis是单线程的，每个命令都会在一个事件循环中执行。在执行Lua脚本期间，Redis会阻塞其他的命令执行，确保Lua脚本能够在一个原子的上下文中执行，不受其他操作的干扰。
                </p>
                <div class="highlight-box">
                    <p class="text-gray-700 flex items-center">
                        <i class="fas fa-lightbulb text-yellow-500 mr-3"></i>
                        单线程模型是Redis保证原子性的核心基础
                    </p>
                </div>
            </div>
        </div>

        <!-- 单线程模型详细图 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <div class="image-container max-w-4xl mx-auto">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1705886671915-9c8cde88-a24e-4746-a8c6-2c812eb8fa56.png?x-oss-process=image%2Fresize%2Cw_937%2Climit_0" 
                     alt="Redis单线程模型" class="w-full">
            </div>
        </div>

        <!-- 事务队列和执行锁定 -->
        <div class="grid md:grid-cols-3 gap-6 mb-12">
            <!-- 事务队列 -->
            <div class="content-card rounded-2xl shadow-xl p-6">
                <div class="text-center mb-4">
                    <div class="feature-icon text-5xl mb-4">
                        <i class="fas fa-list-ol"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3">事务队列的使用</h3>
                </div>
                <p class="text-gray-700 leading-relaxed text-sm">
                    Redis使用事务队列来保证Lua脚本的原子性。在执行Lua脚本时，Redis将脚本放入队列，然后按顺序执行队列中的脚本，确保每个脚本都在原子的上下文中执行。
                </p>
                <div class="image-container mt-4">
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1706002347635-9ee5dfee-8cf4-4d26-80b7-5ebe8bec616d.png" 
                         alt="事务队列" class="w-full">
                </div>
            </div>

            <!-- 执行期间锁定 -->
            <div class="content-card rounded-2xl shadow-xl p-6">
                <div class="text-center mb-4">
                    <div class="feature-icon text-5xl mb-4">
                        <i class="fas fa-lock"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3">执行期间的锁定</h3>
                </div>
                <p class="text-gray-700 leading-relaxed text-sm">
                    在Lua脚本执行期间，Redis会对相关的数据进行锁定，防止其他操作对这些数据进行修改。这种锁定机制确保了Lua脚本的原子性。
                </p>
                <div class="image-container mt-4">
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1706002664797-389b5c2b-7946-4802-a9fb-613de6e901f1.png" 
                         alt="执行锁定" class="w-full">
                </div>
            </div>

            <!-- 不可中断性 -->
            <div class="content-card rounded-2xl shadow-xl p-6">
                <div class="text-center mb-4">
                    <div class="feature-icon text-5xl mb-4">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-3">不可中断性</h3>
                </div>
                <p class="text-gray-700 leading-relaxed text-sm">
                    Lua脚本的执行是不可中断的，即便在执行过程中发生错误，Redis也会确保脚本的执行要么全部成功，要么全部失败。这保证了脚本的一致性。
                </p>
                <div class="highlight-box mt-4">
                    <p class="text-sm text-gray-700">
                        <i class="fas fa-check-circle text-green-500 mr-